﻿using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml.Linq;
using Ionic.Zip;
using Smarly.Idml.Package;
using Smarly.Idml.Package.MasterSpreads;

namespace Smarly.Idml
{
	public class IdmlPackage
	{
		private static readonly object _lock = new object();

		private const string TempFolder = @"d:\temp\_idml";
		private const string TemplateFile = @"D:\Work\Smarly\sources\Smarly.Idml\smarly.idml";

		public static string CreateFile()
		{
			const string resultFile = @"d:\smarly.idml";
			lock (_lock)
			{
				if (Directory.Exists(TempFolder))
					Directory.Delete(TempFolder, true);

				Directory.CreateDirectory(TempFolder);

				if (File.Exists(resultFile))
					File.Delete(resultFile);

				List<Package.PackageFile> files = new List<Package.PackageFile>
					                           {
						                           new Smarly.Idml.Package.MasterSpreads.MasterSpread_u2d5(),
						                           new Smarly.Idml.Package.MasterSpreads.MasterSpread_uc9(),
						                           new Smarly.Idml.Package.MetaInf.Container(),
						                           new Smarly.Idml.Package.MetaInf.Metadata(),
						                           new Smarly.Idml.Package.Resources.Fonts(),
						                           new Smarly.Idml.Package.Resources.Graphic(),
						                           new Smarly.Idml.Package.Resources.Preferences(),
						                           new Smarly.Idml.Package.Resources.Styles(),
						                           new Smarly.Idml.Package.Spreads.Spread_u6b6(),
						                           new Smarly.Idml.Package.Spreads.Spread_u6fb(),
						                           new Smarly.Idml.Package.Spreads.Spread_u792(),
						                           new Smarly.Idml.Package.Spreads.Spread_uc2(),
						                           new Smarly.Idml.Package.Stories.Story_u111(),
						                           new Smarly.Idml.Package.Stories.Story_u14c(),
						                           new Smarly.Idml.Package.Stories.Story_u167(),
						                           new Smarly.Idml.Package.Stories.Story_u17e(),
						                           new Smarly.Idml.Package.Stories.Story_u2dd(),
						                           new Smarly.Idml.Package.Stories.Story_u394(),
						                           new Smarly.Idml.Package.Stories.Story_u3b1(),
						                           new Smarly.Idml.Package.Stories.Story_u447(),
						                           new Smarly.Idml.Package.Stories.Story_u462(),
						                           new Smarly.Idml.Package.Stories.Story_ud1(),
						                           new Smarly.Idml.Package.Stories.Story_uee(),
						                           new Smarly.Idml.Package.Xml.BackingStory(),
						                           new Smarly.Idml.Package.Xml.Tags(),
						                           new Smarly.Idml.Package.DesignMap(),
						                           new Smarly.Idml.Package.Mimetype(),
					                           };

				foreach (var packageFile in files)
				{
					var folder = Path.Combine(TempFolder, packageFile.Folder);
					Directory.CreateDirectory(folder);
					File.WriteAllText(Path.Combine(folder, packageFile.FileName), packageFile.ToResultString());
				}


				using (ZipFile zip = new ZipFile())
				{
					zip.AddDirectory(TempFolder);
					zip.Save(resultFile);
				}

				return resultFile;
			}
		}
	}
}